function [b,ndx,pos] = nanunique(a,flag)%NANUNIQUE Set unique, with a single NaN value for all NaNs in a set.%   Identical in usage and arguments to the standard MatLab function%   unique(). Changes the NaNs to a random value, runs unique on a, and%   changes the random value back to NaN in b. %   Supersedes and replaces unique.m%%   See also UNIQUE, UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER.% RCS INFO: $Id: nanunique.m,v 1.1 1998/12/02 00:08:55 lab Exp lab $%% Jon Goldberg, 5/14/98 - based on mns' nanallequal.m% get the unique nonNaN members of aaq = a(:);ua = unique(aq(finite(aq)));% pick a random value and make sure it is not already in arv = rand;while any(aq==rv)  rv = rand;end% switch the NaNs with the random valueatmp = a;atmp(isnan(a)) = rv;% call unique()if nargin==2  [b,ndx,pos]=unique(atmp,flag);else  [b,ndx,pos]=unique(atmp);end% switch the random value back to NaNb(find(b==rv))=NaN;